package guda.screenshot.frame;

import guda.screenshot.IconLoader;
import guda.screenshot.biz.LoginBiz;
import guda.screenshot.biz.Response;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created by foodoon on 2014/12/28.
 */
public class LoginFrame implements ActionListener {
    JFrame jf;
    JTextField jtf;
    JPasswordField jpf;
    JTextField taskText;
    public LoginFrame() {

        jf = new JFrame("工作记录系统-登录");
        jf.setIconImage(IconLoader.getIconImage());

        jf.setLayout(new GridLayout(5, 1));
        jf.add(new JPanel());
        JLabel jl1 = new JLabel("   用户名:");
        jtf = new JTextField(12);
        JPanel jp1 = new JPanel();
        jp1.add(jl1);
        jp1.add(jtf);
        jf.add(jp1);

        JLabel jl2 = new JLabel("    密   码:");
        jpf = new JPasswordField(12);
        JPanel jp2 = new JPanel();
        jp2.add(jl2);
        jp2.add(jpf);
        jf.add(jp2);

        JLabel taskLabel = new JLabel("任务编号:");
        taskText = new JTextField(12);
        JPanel jp4 = new JPanel();
        jp4.add(taskLabel);
        jp4.add(taskText);
        jf.add(jp4);

        JPanel jp3 = new JPanel();
        JButton jb2 = new JButton("登录");
        jb2.addActionListener(this);
        JButton jb3 = new JButton("取消");
        jb3.addActionListener(this);
        jp3.add(jb2);
        jp3.add(jb3);
        jf.add(jp3);

        jf.setResizable(false);
        jf.setVisible(true);
        jf.setSize(300, 200);
        jf.setLocation(300, 200);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }



    public void actionPerformed(ActionEvent e) {
        String comm = e.getActionCommand();

        if (comm.equals("登录")) {
            if ("".equals(jtf.getText())
                    || "".equals(new String(jpf.getPassword()))
                    || jpf.getPassword() == null || "".equals(taskText.getText())) {
                JOptionPane.showConfirmDialog(
                        jf,
                        "用户名,密码,任务编号不能为空.", "错误",
                        JOptionPane.DEFAULT_OPTION);
                jtf.requestFocus();// 光标回来
            } else {

                LoginBiz loginBiz = new LoginBiz(jtf.getText(),new String(jpf.getPassword()),taskText.getText());
                //校验用户名 密码
                Response login = loginBiz.login();
                if(login.isSuccess()){
                    jf.dispose();
                    new MainFrame(loginBiz);
                }else{
                    JOptionPane.showConfirmDialog(
                            jf,
                            "错误:" + login.getMsg(), "错误",
                            JOptionPane.DEFAULT_OPTION);
                    jtf.requestFocus();
                }


            }
        } else if (comm.equals("取消")) {
            System.exit(0);
        }

    }
}
